import {
	addCategory,
	delCategory,
	updateCategory,
	delTodo,
	addTodo,
	updateTodo
} from "./unicloud.js"
import {
	getAsyncList,
	delAsync
} from "./Async.js"
// 把本地的上传到云端
let upload = async () => {
	const localAsyncData = await getAsyncList()
	// 如果没有找到需要新增的记录
	if (localAsyncData.code == 1) return showToast('没有需要同步的记录')
	const count = countData(localAsyncData)
	count.add > 0 ? await addToCloud(localAsyncData) : showToast('没有需要新增的数据！！')
	await sleep(2000)
	count.del > 0 ? await delToCloud(localAsyncData) : showToast('没有需要删除的数据！！')
	await sleep(2000)
	count.update > 0 ? await updateToCloud(localAsyncData) : showToast('没有需要更新的数据！！')
	await sleep(2000)
	showToast('全部更新完毕')
}
// 本地更新记录

let updateToCloud = async (localAsyncData) => {
	// 筛选出需要更新的同步记录。filter函数返回筛选后的数组，格式和原数组一样
	const updateCategoryArr = localAsyncData.data.filter(function(item) {
		return item.type == 'update';
	})
	const updateTodoArr = localAsyncData.data.filter(function(item) {
		return item.type == 'updatetodo';
	})
	showToast(`需要更新${updateCategoryArr.length}条,现在开始更新`)
	let res
	updateCategoryArr.forEach(async (item) => {
		res = await updateCategory({
			_id: item.docid,
			title: item.data.title
		})
		res.code == 1 ? fail('发生错误') : await success(item._id)
	})
	updateTodoArr.forEach(async (item) => {
		res = await updateTodo({
			_id: item.docid,
			title: item.data.title,
			status: item.data.status
		})
		res.code == 1 ? fail('发生错误') : await success(item._id)
	})
}
// 查找删除记录并上传到云
let delToCloud = async (localAsyncData) => {
	// 筛选出需要删除的同步记录filter函数返回筛选后的数组，格式和原数组一样
	const delCategoryArr = localAsyncData.data.filter(function(item) {
		return item.type == 'del';
	})
	const delTodoArr = localAsyncData.data.filter(function(item) {
		return item.type == 'deltodo';
	})

	let res
	delCategoryArr.forEach(async (item) => {
		res = await delCategory(item.docid)
		res.code == 1 ? fail('发生错误') : await success(item._id)
	})
	await sleep(2000)
	delTodoArr.forEach(async (item) => {
		res = await delTodo(item.docid)
		res.code == 1 ? fail('发生错误') : await success(item._id)
	})
}

// 执行新增记录到云数据操作
let addToCloud = async (localAsyncData) => {
	// 筛选出需要增加的同步记录filter函数返回筛选后的数组，格式和原数组一样
	const localAdd = localAsyncData.data.filter(function(item) {
		return item.type == 'add' || item.type == 'addtodo';
	})
	// 组装数据
	const arrCategory = localAdd.filter((item) => {
		return item.type == 'add'
	})
	const arrTodo = localAdd.filter((item) => {
		return item.type == 'addtodo'
	})
	// 执行上传
	let res
	arrCategory.forEach(async (item) => {
		res = await addCategory(item.data)
		res.code == 1 ? fail('发生错误') : await success(item._id)
	})
	await sleep(2000)
	arrTodo.forEach(async (item) => {
		res = await addTodo(item.data)
		res.code == 1 ? fail('发生错误') : await success(item._id)
	})
	return res
}


const countData = (localAsyncData) => {
	let count = {
		add: 0,
		del: 0,
		update: 0
	}
	localAsyncData.data.forEach((item) => {
		if (item.type == 'add' || item.type == 'addtodo') count.add += 1
		if (item.type == 'del' || item.type == 'deltodo') count.del += 1
		if (item.type == 'update' || item.type == 'updatetodo') count.update += 1
	})
	console.log(count)
	return count
}
const success = async (id) => {
	await delAsync(id)
	showToast(`同步成功，本地记录${id}已删除`)
	console.log(`同步成功，本地记录${id}已删除`)
}
const fail = async (id) => {
	showToast(`同步失败，记录id:${id}`)
}
const showToast = (title, icon = "none") => {
	uni.showToast({
		title,
		icon
	})
}
const sleep = (interval) => {
	return new Promise(resolve => {
		setTimeout(resolve, interval);
	})
}
export {
	upload
}
